﻿using System;
using System.Linq;
using System.Reflection.Emit;
using msilgc.cbuilder.CSyntaxTree;
using msilgc.cbuilder.MSILSyntaxTree;
using msilgc.CSyntaxTree;

namespace msilgc.cbuilder.CBuilder
{
    internal class UnconditionalGotoStatementFactory : ITranslator {
        public ICStatement TranslateAndAdd(CMethodBody methodBody, CBasicBlock currentBlock, BasicBlock block, Instruction instruction) {
            if(instruction.Inst != OpCodes.Br_S &&
                instruction.Inst != OpCodes.Br) {
                throw new Exception("unknown instruction: " + instruction);
            }
            var targetBlock = methodBody.BasicBlocks.First(cBlock => cBlock.MsilBlock == block.Targets[0]);
            var stm = new GotoStatement(targetBlock.Label);
            currentBlock.Statements.Add(stm);
            return stm;
        }
    }
}